package com.gla.giliair.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gla.giliair.dao.PlanemodelDao;
import com.gla.giliair.dao.SeatDao;
import com.gla.giliair.dao.StaticSeatDao;
import com.gla.giliair.entity.*;
import com.gla.giliair.service.PlanemodelService;
import com.gla.giliair.service.SeatService;
import com.gla.giliair.service.StaticSeatService;
import com.gla.giliair.util.BeanUtil;
import com.gla.giliair.util.MapGenerator;
import com.gla.giliair.web.vo.PlanemodelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/25 11:56
 */
@Service
public class PlanemodelServiceImpl implements PlanemodelService {
    @Autowired
    private PlanemodelDao planemodelDao;

    @Autowired
    private StaticSeatDao staticSeatDao;

    @Autowired
    private SeatDao seatDao;

    @Autowired
    private StaticSeatService staticSeatService;

    @Autowired
    private SeatService seatService;

    @Override
    public Planemodel selectPlanemodelById(Long planemodelId) {
        if (planemodelId != null) {
            PlanemodelExample planemodelExample = new PlanemodelExample();
            planemodelExample.or().andPlanemodelIdEqualTo(planemodelId).andIsDeletedEqualTo(0);
            List<Planemodel> planemodelList = planemodelDao.selectByExample(planemodelExample);
            if (planemodelList.isEmpty()) {
                return null;
            }
            return planemodelList.get(0);
        }
        return null;
    }

    @Override
    public List<Planemodel> selectAllPlanemodels(PlanemodelExample orderBy, boolean showDeleted) {
        if (!showDeleted) {
            orderBy.or().andIsDeletedEqualTo(0);
        }
        List<Planemodel> planemodelList = planemodelDao.selectByExample(orderBy);
        return planemodelList;
    }

    @Override
    public List<Planemodel> selectPlanemodelsByParams(PlanemodelExample orderBy, boolean showDeleted, PlanemodelVO searchParams) {
        PlanemodelExample.Criteria criteria = orderBy.createCriteria();
        if (!showDeleted) {
            criteria.andIsDeletedEqualTo(0);
        }
        if (StringUtils.hasText(searchParams.getPlanemodelName())) {
            criteria.andPlanemodelNameLike("%" + searchParams.getPlanemodelName() + "%");
        }
        List<Planemodel> planemodelList = planemodelDao.selectByExample(orderBy);
        return planemodelList;
    }

    @Override
    public Map<String, Object> findAllPlanemodels(String sort, boolean isASC, boolean showDeleted, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Planemodel> planemodelList = selectAllPlanemodels(planemodelSort(sort, isASC), showDeleted);
        PageInfo pageInfo = new PageInfo(planemodelList);
        if (!planemodelList.isEmpty()) {
            List<PlanemodelVO> planemodelVOList = BeanUtil.copyList(planemodelList, PlanemodelVO.class);
            pageInfo.setList(planemodelVOList);
            return MapGenerator.getResWithData(200, "查询飞机机型列表成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "查询结果为空！");
        }
    }

    @Override
    public Map<String, Object> addPlanemodel(Planemodel planemodel) {
        PlanemodelExample planemodelExample = new PlanemodelExample();
        planemodelExample.or().andPlanemodelNameEqualTo(planemodel.getPlanemodelName()).andIsDeletedEqualTo(0);
        List<Planemodel> planemodelList = planemodelDao.selectByExample(planemodelExample);
        if (!planemodelList.isEmpty()) {
            return MapGenerator.getRes(502, "飞机机型名称已被占用！");
        }
        //填充创建、更新日期
        Date nowDate = new Date();
        planemodel.setCreateTime(nowDate);
        planemodel.setUpdateTime(nowDate);
        if (planemodelDao.insertSelective(planemodel) > 0) {
            return MapGenerator.getRes(200, "添加飞机机型成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    @Override
    public Map<String, Object> deletePlanemodel(Long planemodelId, boolean isSeatDeleted) {
        Planemodel planemodelFromDB = selectPlanemodelById(planemodelId);
        if (planemodelFromDB == null) {
            return MapGenerator.getRes(501, "飞机机型不存在！");
        }
        planemodelFromDB.setIsDeleted(1);
        planemodelFromDB.setUpdateTime(null);
        if (planemodelDao.updateByPrimaryKeySelective(planemodelFromDB) > 0) {
            if (isSeatDeleted) {
                //只删除静态座位，相关动态座位人工查证删除（自动全删毁数据）
                List<StaticSeat> staticSeatList = staticSeatService.selectStaticSeatsByPlanemodelId(planemodelId);
                for (StaticSeat staticSeat : staticSeatList) {
                    if (staticSeatDao.deleteByPrimaryKey(staticSeat.getStaticSeatId()) <= 0) {
                        return MapGenerator.getRes(520, "内部错误！");
                    }
                }
//                List<Seat> seatList = seatService.;
//                for (Seat seat : seatList) {
//                    if (seatDao.deleteByPrimaryKey(seat.getSeatId()) <= 0) {
//                        return MapGenerator.getRes(520, "内部错误！");
//                    }
//                }
            }
            return MapGenerator.getRes(200, "删除飞机机型成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    @Override
    public Map<String, Object> searchPlanemodels(String sort, boolean isASC, boolean showDeleted, Integer page, Integer pageSize, PlanemodelVO searchParams) {
        if (searchParams == null) {
            return MapGenerator.getRes(500, "搜索参数为空！");
        }
        PageHelper.startPage(page, pageSize);
        List<Planemodel> planemodelList = selectPlanemodelsByParams(planemodelSort(sort, isASC), showDeleted, searchParams);
        PageInfo pageInfo = new PageInfo(planemodelList);
        if (!planemodelList.isEmpty()) {
            List<PlanemodelVO> planemodelVOList = BeanUtil.copyList(planemodelList, PlanemodelVO.class);
            pageInfo.setList(planemodelVOList);
            return MapGenerator.getResWithData(200, "查询飞机机型列表成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "查询结果为空！");
        }
    }

    //排序参数转化
    private PlanemodelExample planemodelSort(String sort, boolean isASC) {
        PlanemodelExample planemodelExample = new PlanemodelExample();
        sort = sort == null ? "" : sort;
        String column = "";
        String order = "desc";
        if (isASC) {
            order = "asc";
        }
        switch (sort) {
            case "planemodelId":
                column = "planemodel_id";
                break;
            case "planemodelName":
                column = "planemodel_name";
                break;
            case "firstClassTotality":
                column = "first_class_totality";
                break;
            case "businessClassTotality":
                column = "business_class_totality";
                break;
            case "economyClassTotality":
                column = "economy_class_totality";
                break;
            case "createTime":
                column = "create_time";
                break;
            case "updateTime":
                column = "update_time";
                break;
            case "isDeleted":
                column = "is_deleted";
                break;
            default:
        }
        if ("".equals(column)) {
            planemodelExample.setOrderByClause("planemodel_id desc");
        } else {
            if ("company_id".equals(column)) {
                planemodelExample.setOrderByClause(String.format("%s %s", column, order));
            } else {
                planemodelExample.setOrderByClause(String.format("%s %s , planemodel_id desc", column, order));
            }
        }
        return planemodelExample;
    }
}
